using System;
using System.Threading;
using System.Collections.Generic;
using OpenMetaverse;

namespace OpenFuse.Backend.Commands
{
	public class DownloadFileCommand : Command
	{
		public delegate void Result(SlSession session, Cache.FileNode file, Cache.Asset asset);
		
		Result _resultCallback;
		Cache.FileNode _file;
		
		public DownloadFileCommand(SlSession session, Cache.FileNode file, Result resultCallback) : base(session)
		{
			_file = file;
			_resultCallback = resultCallback;
		}

		public override void Run()
		{
			_session.Logger.Debug("Start DownloadFileCommand " + _file.Name);

			Cache.Asset asset = _session.Cache.Assets.GetByAssetId(_file.AssetId);
			if(asset == null)
				asset = Downloaders.Downloader.Download(_session, _file.Parent.Id, _file.Id);
			
			_resultCallback(_session, _file, asset);

			_session.Logger.Debug("End DownloadFileCommand");
		}
	}
}
